package com.jinkeen.xiaxie.chatRoom

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.jinkeen.xiaxie.databinding.ActivityChatRoomMainBinding

class ChatRoomMainActivity : AppCompatActivity() {
    var binding: ActivityChatRoomMainBinding? = null
    var fragments = ArrayList<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityChatRoomMainBinding.inflate(layoutInflater)
        setContentView(binding!!.root)

        fragments.add(ChatFragment())
        fragments.add(FriendsFragment())
        fragments.add(DiscoverFragment())
        fragments.add(MeInfoFragment())
        binding!!.chatViewPage.adapter = ChatAdapter(this, fragments)

        //BottomNavigationView的点击切换fragment
        binding!!.bottom.setOnNavigationItemReselectedListener { item ->
            binding!!.chatViewPage.currentItem = item.itemId
        }
    }

    private inner class ChatAdapter(fa: FragmentActivity, private val fragments: ArrayList<Fragment>) : FragmentStateAdapter(fa) {
        override fun getItemCount(): Int = fragments.size
        override fun createFragment(position: Int): Fragment = fragments[position]
    }
}